﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Server_NhaSach.DataAccess.HelperLayer;
using System.Data.SqlClient;
using Server_NhaSach.DataTransferObject;
using System.Data;

namespace Server_NhaSach.DataAccess.LogicLayer
{
    public class MatHangDAO
    {
    
        #region "Khai báo các biến"
        private Helper helper;
        private string _error;
        private MatHangDTO _mathang;
        #endregion

        #region "Khai báo các thuộc tính"
        public string Error
        {
            get { return _error; }
            set { _error = value; }
        }
        public MatHangDTO Mathang
        {
            get { return _mathang; }
            set { _mathang = value; }
        }
        #endregion

        #region "Khai báo các Method"
        public bool InsertMatHang(MatHangDTO MatHang)
        {
            helper = new Helper();
            SqlParameter[] par = new SqlParameter[15];
            par[0] = new SqlParameter("@MaMH", MatHang.MaMH);
            par[1] = new SqlParameter("@MaLoai", MatHang.Maloai);
            par[2] = new SqlParameter("@TenMH", MatHang.TenMH);
            par[3] = new SqlParameter("@DonGiaNhap", MatHang.Dongianhap);
            par[4] = new SqlParameter("@DonGiaSi", MatHang.Dongiasi);
            par[5] = new SqlParameter("@DonGiaLe", MatHang.Dongiale);
            par[6] = new SqlParameter("@khuyenmai", MatHang.Khuyenmai);
            par[7] = new SqlParameter("@NhaSX", MatHang.Nhasx);
            par[8] = new SqlParameter("@TacGia", MatHang.Tacgia);
            par[9] = new SqlParameter("@NamXuatBan", MatHang.Namsanxuat);
            par[10] = new SqlParameter("@DauSach", MatHang.Dausach);
            par[11] = new SqlParameter("@DVT", MatHang.Dvt);
            par[12] = new SqlParameter("@SoLuongDauKy", MatHang.Soluongtondauky);
            par[13] = new SqlParameter("@SoLuongHienCo", MatHang.Soluonghienco);
            par[14] = new SqlParameter("@Hinh", MatHang.Hinh);
            try
            {
                helper.InsertQuery("sp_Insert_MatHang", par);
            }
            catch (Exception ex)
            {
                _error = helper.Error + " - " + ex.Message;
                return false;
            }
            return true;
        }
        public bool UpdateMatHang(MatHangDTO MatHang)
        {
            helper = new Helper();
            SqlParameter[] par = new SqlParameter[15];
            par[0] = new SqlParameter("@MaMH", MatHang.MaMH);
            par[1] = new SqlParameter("@MaLoai", MatHang.Maloai);
            par[2] = new SqlParameter("@TenMH", MatHang.TenMH);
            par[3] = new SqlParameter("@DonGiaNhap", MatHang.Dongianhap);
            par[4] = new SqlParameter("@DonGiaSi", MatHang.Dongiasi);
            par[5] = new SqlParameter("@DonGiaLe", MatHang.Dongiale);
            par[6] = new SqlParameter("@khuyenmai", MatHang.Khuyenmai);
            par[7] = new SqlParameter("@NhaSX", MatHang.Nhasx);
            par[8] = new SqlParameter("@TacGia", MatHang.Tacgia);
            par[9] = new SqlParameter("@NamXuatBan", MatHang.Namsanxuat);
            par[10] = new SqlParameter("@DauSach", MatHang.Dausach);
            par[11] = new SqlParameter("@DVT", MatHang.Dvt);
            par[12] = new SqlParameter("@SoLuongDauKy", MatHang.Soluongtondauky);
            par[13] = new SqlParameter("@SoLuongHienCo", MatHang.Soluonghienco);
            par[14] = new SqlParameter("@Hinh", MatHang.Hinh);
            try
            {
                helper.UpdateQuery("sp_Update_MatHang", par);
            }
            catch (Exception ex)
            {
                _error = helper.Error + " - " + ex.Message;
                return false;
            }
            return true;
        }
        public bool DeleteMatHang(string MaMH)
        {
            helper = new Helper();
            SqlParameter[] par = new SqlParameter[1];
            par[0] = new SqlParameter("@MaMH", MaMH);
            try
            {
                helper.DeleteQuery("sp_Delete_MatHang", par);
            }
            catch (Exception ex)
            {
                _error = helper.Error + " - " + ex.Message;
                return false;
            }
            return true;
        }
        public DataSet LayDS_MatHang()
        {
            helper = new Helper();
            DataSet ds_mh = helper.SelectQuery("sp_LayDS_MatHang");
            return ds_mh;
        }

        #endregion        
    }
}